package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.GameZhenHuanPromotionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class GameZhenHuanPromotionDaoImpl extends TempDataObjectDao<GameZhenHuanPromotionData> {
    public static String getGameZhenHuanPromotionDataSql = "select * from `t_u_zhenhuan_promotion` where `activityId`=?";
    public GameZhenHuanPromotionData getGameZhenHuanPromotionData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGameZhenHuanPromotionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGameZhenHuanPromotionDataSql = "update `t_u_zhenhuan_promotion` set promotionInfoMap=? where activityId=? and childType=?";
    @Override
    protected boolean updateData(GameZhenHuanPromotionData gameZhenHuanPromotionData) {
        gameZhenHuanPromotionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gameZhenHuanPromotionData.getPromotionInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanPromotionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanPromotionData.getChildType()));
        if (getDb().executeNoneQuery(updateGameZhenHuanPromotionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGameZhenHuanPromotionDataSql = "insert into `t_u_zhenhuan_promotion`(activityId,childType,promotionInfoMap) values (?,?,?)";
    @Override
    protected boolean addData(GameZhenHuanPromotionData gameZhenHuanPromotionData) {
        gameZhenHuanPromotionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanPromotionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanPromotionData.getChildType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gameZhenHuanPromotionData.getPromotionInfoMap())));
        if (getDb().executeNoneQuery(insertGameZhenHuanPromotionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GameZhenHuanPromotionData parseData(ResultSet rs) throws SQLException {
        GameZhenHuanPromotionData gameZhenHuanPromotionData = new GameZhenHuanPromotionData();
        gameZhenHuanPromotionData.setActivityId(rs.getInt("activityId"));
        gameZhenHuanPromotionData.setChildType(rs.getInt("childType"));
        gameZhenHuanPromotionData.setPromotionInfoMap(JSON.parseObject(rs.getString("promotionInfoMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.zhenhuan.ZhenHuanPromotionInfo>>() {}));

        return gameZhenHuanPromotionData;
    }
}
